package com.itheima.jobs;

import com.itheima.constant.RedisConstant;
import com.itheima.utils.QiniuUtils;
import org.springframework.beans.factory.annotation.Autowired;
import redis.clients.jedis.Jedis;
import redis.clients.jedis.JedisPool;

import java.util.Set;

public class ClearImgJob {

    @Autowired
    private JedisPool jedisPool;

    public void clearImg() {
        Jedis resource = null;
        try {
            resource = jedisPool.getResource();
            //多的那个要放到前面 多的图片名称集合删除  成功上传套餐的图片集合  获得上传的垃圾图片
            Set<String> sdiff = resource.sdiff(RedisConstant.SETMEAL_PIC_RESOURCES, RedisConstant.SETMEAL_PIC_DB_RESOURCES);

            if (sdiff != null) {
                //遍历垃圾图片集合
                for (String fileName : sdiff) {
                    //调用工具类删除七牛云上的垃圾图片
                    QiniuUtils.deleteFileFromQiniu(fileName);
                    //从redis集合中删除图片名称
                    resource.srem(RedisConstant.SETMEAL_PIC_RESOURCES, fileName);
                }
            }
        } catch (Exception e) {
            e.printStackTrace();
        } finally {
            if (resource != null) {
                resource.close();
            }
        }
    }
}
